﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECare.BUS;
using ECare.BUS.Data;
using ECare.Client.frms;

namespace ECare.Win.controls
{
    public partial class ucOutNew : BaseModule
    {
        public ucOutNew()
        {
            InitializeComponent();


            this.windowsUIButtonPanel1.ButtonClick += windowsUIButtonPanel1_ButtonClick;
            this.Load += ucOutNew_Load;
            this.grd.CellDoubleClick += grd_CellDoubleClick;
        }

        void grd_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.grd.SelectedRows.Count == 0)
                return;
            var entitye = this.grd.SelectedRows[0].DataBoundItem as ResourceOutInfo;

            var frm = new frmOutFormReadonly();
            frm.entity = entitye;
            frm.ShowDialog();}


        public BindingList<ResourceOutInfo> source { get; set; }

        void ucOutNew_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();

            resource_id.DataSource = ResourcesHelper.GetAllResources();
            resource_id.DisplayMember = "ResourceName";
            resource_id.ValueMember = "ResourceId";


            var users = UserInfoHelper.GetUsers();

            resource_get_by.DataSource = users;
            resource_get_by.DisplayMember = "UserName";
            resource_get_by.ValueMember = "UserId";

            resource_create_by.DataSource = users;
            resource_create_by.DisplayMember = "UserName";
            resource_create_by.ValueMember = "UserId";

            //zhuangtai

            // resource_status.DataSource =



            //设置出库单的数据源
            this.source = new BindingList<ResourceOutInfo>(ResourceOutHelper.GetOutINfos("1=1"));
            this.grd.DataSource = this.source;


        }

        void windowsUIButtonPanel1_ButtonClick(object sender, DevExpress.XtraBars.Docking2010.ButtonEventArgs e)
        {
            //throw new NotImplementedException();

            if (e.Button.Properties.Caption == "New")
            {
                this.CreateNew();
            }
            else if (e.Button.Properties.Caption == "Edit")
            {
                Edit();
            }
            else if (e.Button.Properties.Caption == "Cancel")
            {
                this.Cancel();
            }


        }

        private void Cancel()
        {
            if (this.grd.SelectedRows.Count == 0)
                return;
            var entitye = this.grd.SelectedRows[0].DataBoundItem as ResourceOutInfo;

            var frm = new frmOutFormReadonly();
            frm.Deactived += frm_Deactived;
            frm.entity = entitye;
            frm.ShowDialog();
        }

        void frm_Deactived(object entity)
        {
            //throw new NotImplementedException();
            source.Remove(entity as ResourceOutInfo);
        }

        private void CreateNew()
        {
            var frm = new frmOutInfoNew();
            frm.Saved += frm_Saved;
            frm.ShowDialog();
        }

        private void Edit()
        {

            if (this.grd.SelectedRows.Count == 0)
                return;
            var entitye = this.grd.SelectedRows[0].DataBoundItem as ResourceOutInfo;

            if (entitye.resource_status != CheckStatus.Pending)
            {
                MessageBox.Show("当前订单已经被审核或者出库，不能修改");
                return;
            }


            var frm = new frmOutInfoNew();
            frm.Saved +=frm_Saved;
            frm.entity = entitye;
            frm.ShowDialog();


        }



        void frm_Saved(object entity)
        {
            if (source == null)
                source = new BindingList<ResourceOutInfo>();
            var role = entity as ResourceOutInfo;
            var findresult = source.FirstOrDefault(a => a.resource_out_id == role.resource_out_id);
            if (findresult == null)
                this.source.Add(role);


            this.grd.Refresh();

        }

    }
}
